package com.wj.dubbo.service.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wj.console.common.exception.BusinessException;
import com.wj.console.constant.Enums.RESULT_CODE;
import com.wj.console.rabbit.message.CrawlerSocialFundMessage;

public class CrawlerTask implements Runnable {
	
	private static final Logger log = LoggerFactory.getLogger(CrawlerTask.class);
	private static int PAUSE_MINUTE = 10;//服务暂停分钟
	private ITaskService taskService;
	private CrawlerSocialFundMessage message;
	
	
	public CrawlerTask(ITaskService taskService, CrawlerSocialFundMessage message){
		this.taskService = taskService;
		this.message = message;
	}
	
	@Override
	public void run() {
		do{
			try {
				taskService.runTask(message);
			} catch (BusinessException e) {
				log.error("任务处理业务异常", e);
				//如果是系统维护则当前任务阻塞
				if(RESULT_CODE.SYSTERM_MAINTAIN.key.equals( e.getCode())){
					taskService.pauseTask(null);
					//暂停一定时间
					taskService.pause(PAUSE_MINUTE);
				}
			}catch (Throwable e1) {
				log.error("任务处理系统异常", e1);
			}
		}while(taskService.isPaused());
		
		//任务处理完成之后，如果是暂停状态，则启动任务
		if(taskService.isPaused()){
			taskService.startTask();
		}
		
	}

}
